﻿// JScript File
/// <reference path="../BaseObject.js" />

/*****************************************************************************
Ajax library class definition. Kevin Robinson 2006.
Class: ajax.lang.ResourceCache
/*****************************************************************************/


ajax.lang.addNamespace("ajax.lang");

//-----------------------------------------------------------------------------
ajax.lang.ResourceCache = function() // Constructor
{        
    // The instance items are shared across all instances.
    this.items      = ajax.lang.ResourceCache.items;
    this.owner      = null;    
}
//-----------------------------------------------------------------------------
ajax.lang.extend("ajax.lang.ResourceCache", "ajax.BaseObject"); // Inherit

//-----------------------------------------------------------------------------
ajax.lang.ResourceCache.prototype.getFile = function(sPath)
{   
   return ajax.lang.ResourceCache.getFile(sPath);
}

//-----------------------------------------------------------------------------
ajax.lang.ResourceCache.prototype.createPathRef = function(sPath)
{
    return ajax.lang.ResourceCache.createPathRef(sPath);
}

//-----------------------------------------------------------------------------
ajax.lang.ResourceCache.prototype.toString = function()
{
    return "ajax.lang.ResourceCache";
}

//-----------------------------------------------------------------------------
ajax.lang.ResourceCache.prototype.toClassName = function()
{
    return ajax.lang.getClassPath("ajax.lang.ResourceCache");
}

//-----------------------------------------------------------------------------
ajax.lang.ResourceCache.cast = function(obj)
{///<returns type="ajax.lang.ResourceCache"/>
    return obj;
}


//-----------------------------------------------------------------------------
// Statics
ajax.lang.ResourceCache.createPathRef = function(sPath)
{
    var sRef = sPath.replace(/\//ig, "");
    sRef = sRef.replace(/\./ig, "");    
    return sRef;
}

//-----------------------------------------------------------------------------
ajax.lang.ResourceCache.items = new Object();

//-----------------------------------------------------------------------------
ajax.lang.ResourceCache.getFile = function(sPath)
{          
    // Create a ref that gets rid of special chars
    var sRef = ajax.lang.ResourceCache.createPathRef(sPath);
    
    // if it already exists, return it    
    if (ajax.lang.ResourceCache.items[sRef])
        return ajax.lang.ResourceCache.items[sRef];

    // No exist, then load it, add it and return it

    var loader = new ajax.net.ContentLoader(this, null, null, sPath, "GET", null, false);
    var req    = loader.sendRequest();
    // Store the resource and return it to the caller
    ajax.lang.ResourceCache.items[sRef] = req.responseText;
    return ajax.lang.ResourceCache.items[sRef]; 

}




